//
//  XMGTabBarController.m
//  BuDeJie19
//
//  Created by hjj on 2016/12/3.
//  Copyright © 2016年 hjjuny. All rights reserved.
//
#import "XMGTabBarController.h"
#import "XMGEssenceViewController.h"
#import "XMGNewViewController.h"
#import "XMGFriTrendViewController.h"
#import "XMGMeViewController.h"
#import "XMGPublishViewController.h"
#import "UIImage+Rendar.h"
#import "UITabBarItem+Font.h"
#import "XMGNavigationController.h"

/*
    封装：谁的事情谁管理，让业务逻辑非常清晰
 */

/*
    1.给UITabBar里面的按钮设置内容 => UITabBar里面的按钮由对应的子控制器的tabBarItem决定
 */

/*
    iOS7之后，UITabBar上的按钮只要被选中，就会默认被渲染(tintColor)
    1.按钮被选中，图片被渲染 1.直接修改图片 2.通过代码
    2.按钮被选中，文字颜色 和 字体
    3.中间发布按钮显示不出来 : 图片被渲染
 
    中间图片为什么会被渲染？中间图片太大，系统就自动渲染
 */

/*
    系统UITabBarButton,只有选中
 */
@interface XMGTabBarController ()<UITabBarControllerDelegate>
@property (nonatomic, weak) UIViewController *selectVc;
@property (nonatomic, weak) UIButton *plusButton;

@end

@implementation XMGTabBarController

// 程序一启动就会把加载类到内存
// 调用一次
+ (void)load
{
    // 获取UITabBarItem外观
    UITabBarItem *item = [UITabBarItem appearance];
    
    // 设置系统UITabBarButton的字体 =》 tabBarItem
    // 富文本属性：文字字体，颜色，图文hui排（html5）
    // 字体
    // 字体只能通过正常状态去设置
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    [item setTitleTextAttributes:attr forState:UIControlStateNormal];
    
     // item.title = @"123";
    
    /*
        appearance
        1.谁可以使用appearance 必须遵守UIAppearance协议，实现协议方法
        2.使用appearance，可以设置任何属性 不可以
        2.1 哪些属性可以通过appearance设置 ， 只有带有UI_APPEARANCE_SELECTOR宏的属性或者方法，才可以通过appearance调用
        3.如果通过appearance设置属性，必须要在显示之前设置
     */
}

// 第一次使用当前类或者子类的时候也会调用
// 可能会调用多次
//+ (void)initialize
//{
//    if (self == [XMGTabBarController class]) {
//        
//    }
////    static dispatch_once_t onceToken;
////    dispatch_once(&onceToken, ^{
////        
////    });
//    
//}
- (UIButton *)plusButton
{
    if (_plusButton == nil) {
        // 创建加号按钮
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        _plusButton = btn;
        [btn setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
        // 自适应
        [btn sizeToFit];
        
        [self.tabBar addSubview:btn];
    }
    return _plusButton;
}

/*
    系统的UITabBarButton什么时候添加的,并不是在viewDidLoad添加
 
    UITabBarButton可能在viewWillAppear
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
    
    self.plusButton.center = CGPointMake(self.tabBar.bounds.size.width * 0.5, self.tabBar.bounds.size.height * 0.5);
    
    self.tabBar.tintColor = [UIColor blackColor]; // 设置渲染颜色
 
    // 添加所有的子控制器
    [self addAllChildViewController];
    
    // 设置UITabBar上所有按钮内容
    [self setupAllTabBarButton];
    
    _selectVc = self.childViewControllers[0];
}

#pragma mark - UITabBarControllerDelegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    // 重复点击
    if (_selectVc == viewController) {
        // 刷新当前控制器
        // 怎么拿全部控制器
        // 通知：跨层去做事情，
        [[NSNotificationCenter defaultCenter] postNotificationName:@"repeatClickTab" object:nil];
    }
    _selectVc = viewController;
    
}

// 验证UITabBarButton在viewWillAppear
// 技巧：如何判断一个方法做了神马事情，直接干掉
//- (void)viewWillAppear:(BOOL)animated
//{
//    
//     [super viewWillAppear:animated];
//    // NSLog(@"%@",self.tabBar.subviews);
//}

/*
    想让自己加载图片的方法也有插件提示 =》 修改插件 =》 寻找插件 => 执行完插件源码，就有插件 => install => 
 */

#pragma mark - 设置UITabBar上所有按钮内容
- (void)setupAllTabBarButton
{
    // 0 => tabBarVc的第0个子控制器
    UINavigationController *nav = self.childViewControllers[0];
    nav.tabBarItem.title = @"精华";
    // 如果是通过导航控制器的子控制器去设置，必须要同时设置标题和图片
//    XMGEssenceViewController *vc = nav.topViewController;
//    vc.tabBarItem.title = @"精华";
    nav.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
    nav.tabBarItem.selectedImage = [UIImage imageNamedWithOriginal:@"tabBar_essence_click_icon"];
    
    // 1
    UINavigationController *nav1 = self.childViewControllers[1];
    nav1.tabBarItem.title = @"新帖";
    nav1.tabBarItem.image = [UIImage imageNamed:@"tabBar_new_icon"];
    nav1.tabBarItem.selectedImage = [UIImage imageNamedWithOriginal:@"tabBar_new_click_icon"];
    
    // 2
    UIViewController *vc = self.childViewControllers[2];
    vc.tabBarItem.enabled = NO;
//    vc.tabBarItem.image = [UIImage imageNamedWithOriginal:@"tabBar_publish_icon"];
//    vc.tabBarItem.selectedImage = [UIImage imageNamedWithOriginal:@"tabBar_publish_click_icon"];
//    // 设置中间图片位置
//    vc.tabBarItem.imageInsets = UIEdgeInsetsMake(7, 0, -7, 0);
    
    // 3
    UINavigationController *nav3 = self.childViewControllers[3];
    nav3.tabBarItem.title = @"关注";
    nav3.tabBarItem.image = [UIImage imageNamed:@"tabBar_friendTrends_icon"];
    nav3.tabBarItem.selectedImage = [UIImage imageNamedWithOriginal:@"tabBar_friendTrends_click_icon"];
    
    // 4
    UINavigationController *nav4 = self.childViewControllers[4];
    nav4.tabBarItem.title = @"我";
    nav4.tabBarItem.image = [UIImage imageNamed:@"tabBar_me_icon"];
    nav4.tabBarItem.selectedImage = [UIImage imageNamedWithOriginal:@"tabBar_me_click_icon"];
    
}

// cmd + optional + ←
#pragma mark - 添加所有的子控制器
- (void)addAllChildViewController
{
    // 添加导航控制器为tabBarVc子控制器
    // tabBarVc默认会把第0个子控制器的View添加上去
    // 精华
    XMGEssenceViewController *vc = [[XMGEssenceViewController alloc] init];
    // 创建导航控制器
    // 导航控制器会把根控制器的View添加到自己的View
    // initWithRootViewController:pushViewController
    // 导航控制器push,把控制器压入栈
    // 导航控制器会把栈顶控制器的View添加上去 => 导航控制器显示到最外面的View,栈顶控制器View
    XMGNavigationController *nav = [[XMGNavigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
    
    // 新帖
    XMGNewViewController *vc1 = [[XMGNewViewController alloc] init];
    
    XMGNavigationController *nav1 = [[XMGNavigationController alloc] initWithRootViewController:vc1];
    [self addChildViewController:nav1];
    
    // 发布
    UIViewController *vc2 = [[UIViewController alloc] init];
    vc2.view.backgroundColor = [UIColor blueColor];
    [self addChildViewController:vc2];
    
    // 关注
    XMGFriTrendViewController *vc3 = [[XMGFriTrendViewController alloc] init];
    
    XMGNavigationController *nav3 = [[XMGNavigationController alloc] initWithRootViewController:vc3];
    [self addChildViewController:nav3];
    
    // 我
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"XMGMeViewController" bundle:nil];
    XMGMeViewController *vc4 = [storyboard instantiateInitialViewController];
    XMGNavigationController *nav4 = [[XMGNavigationController alloc] initWithRootViewController:vc4];
    [self addChildViewController:nav4];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
